package org.funismycompta.dao.hibernate;

import java.util.List;

import org.apache.log4j.Logger;
import org.funismycompta.dao.CompteDao;
import org.funismycompta.modele.Account;
import org.funismycompta.testu.DaoTestU;
import org.funismycompta.testu.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class CompteDaoImpl implements CompteDao {

	private static final Logger logger = Logger.getLogger(DaoTestU.class);

	public void creerCompte(Account c) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tansaction = null;
		try {
			tansaction = session.beginTransaction();

			session.save(c);
			logger.debug("COMPTE SAUVE");
			// commit
			tansaction.commit();
			logger.debug("COMMIT");
		} catch (RuntimeException e) {
			if (tansaction != null)
				tansaction.rollback();
			logger
					.debug(e.getMessage()
							+ "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			throw e;
		} finally {
			logger.debug("CLOSE");
			session.close();
		}

	}

	@Override
	public Account getCompte(Long id) {
		Account compte = null;
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tansaction = null;
		try {
			tansaction = session.beginTransaction();

			compte = (Account) session.get(Account.class, id);
		} catch (RuntimeException e) {
			if (tansaction != null)
				tansaction.rollback();
			logger
					.debug(e.getMessage()
							+ "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			throw e;
		} finally {
			logger.debug("CLOSE");
			session.close();
		}
		return compte;
	}

	@Override
	public List<Account> getCompteDuClient(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void supprimerCompte(Account c) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();

			session.delete(c);

			transaction.commit();
		} catch (RuntimeException e) {
			if (transaction != null)
				transaction.rollback();
			logger
					.debug(e.getMessage()
							+ "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			throw e;
		} finally {
			logger.debug("CLOSE");
			session.close();
		}
	}

	@Override
	public void updateAccount(Account c) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();

			session.update(c);
			transaction.commit();
		} catch (RuntimeException e) {
			if (transaction != null)
				transaction.rollback();
			logger
					.debug(e.getMessage()
							+ "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			throw e;
		} finally {
			logger.debug("CLOSE");
			session.close();
		}

	}

	@Override
	public void creditAccount(Long id, double amount) {
		try {
			Account account = this.getCompte(id);
			double bankBalance = account.getBankBalance() + amount;
			account.setBankBalance(bankBalance);
			this.updateAccount(account);
		} catch (RuntimeException e) {
			logger.debug(e.getMessage()+ "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			throw e;
		}
	}

}
